﻿<%@ Page Language="C#" MasterPageFile="~/MasterPages/Default.Master" AutoEventWireup="true"
  CodeBehind="AccessoryInfo.aspx.cs" Inherits="SaleManagement.Web.Pages.AccessoryInfo"
  Title="" %>

<%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dxcb" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dxpc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <div id="divContent" runat="server">
    <table width="100%">
      <tr>
        <td class="search_header">
          Thông tin hóa đơn nhập nguyên vật liệu
        </td>
      </tr>
      <tr>
        <td style="text-align: right">
          <dxe:ASPxHyperLink ID="hlBack" runat="server" Text="Trở về" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
            CssPostfix="Aqua" Cursor="pointer">
            <ClientSideEvents Click="function(s, e) {
	onBack();
}" />
          </dxe:ASPxHyperLink>
        </td>
      </tr>
      <tr>
        <td class="insert_header">
          Thông tin chung
        </td>
      </tr>
      <tr>
        <td class="formInfo">
          <table border="0" width="100%" style="padding-bottom: 20px">
            <tr>
              <td style="width: 100px">
                Mã hóa đơn:
              </td>
              <td style="width: 170px">
                <dxe:ASPxTextBox ID="txtRefNo" runat="server" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                  CssPostfix="Aqua" Width="170px" ClientEnabled="False" ClientInstanceName="txtRefNo">
                  <ValidationSettings>
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                </dxe:ASPxTextBox>
              </td>
              <td style="width: 50px">
              </td>
              <td style="width: 100px">
                Ngày nhập NVL:
              </td>
              <td style="width: 170px">
                <dxe:ASPxDateEdit ID="deImportDate" ClientInstanceName="deImportDate" runat="server"
                  CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" ImageFolder="~/App_Themes/Aqua/{0}/"
                  ShowShadow="False" Width="170px">
                  <ValidationSettings>
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" Width="14px" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                  <DropDownButton>
                    <Image Height="7px" Url="~/App_Themes/Aqua/Editors/edtDropDown.png" UrlDisabled="~/App_Themes/Aqua/Editors/edtDropDownDisabled.png"
                      UrlHottracked="~/App_Themes/Aqua/Editors/edtDropDownHottracked.png" UrlPressed="~/App_Themes/Aqua/Editors/edtDropDownHottracked.png"
                      Width="9px" />
                  </DropDownButton>
                  <CalendarProperties>
                    <HeaderStyle Spacing="1px" />
                    <FooterStyle Spacing="17px" />
                  </CalendarProperties>
                </dxe:ASPxDateEdit>
              </td>
              <td class="notNullNote">
                (*)
              </td>
            </tr>
            <tr>
              <td>
                Tổng tiền:
              </td>
              <td>
                <dxe:ASPxTextBox ID="txtTotalAmount" ClientInstanceName="txtTotalAmount" runat="server"
                  CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" Width="170px"
                  ClientEnabled="False">
                  <ValidationSettings Display="Dynamic">
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                  <MaskSettings IncludeLiterals="None" Mask="&lt;0..9999999999999999999g&gt;" />
                </dxe:ASPxTextBox>
              </td>
              <td>
              </td>
            </tr>
            <tr>
              <td>
                Ghi chú:
              </td>
              <td colspan="5">
                <dxe:ASPxMemo ID="mmNote" runat="server" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                  CssPostfix="Aqua" Height="71px" Width="495px">
                  <ValidationSettings>
                    <ErrorImage Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                </dxe:ASPxMemo>
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td class="insert_header" style="height: 24px">
          Thông tin chi tiết
        </td>
      </tr>
      <tr>
        <td class="formInfo">
          <table width="100%">
            <tr>
              <td>
                <dxwgv:ASPxGridView ID="gvAccessoryDetail" ClientInstanceName="gvAccessoryDetail"
                  KeyFieldName="Id" runat="server" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                  CssPostfix="Aqua" Width="100%" EnableCallbackCompression="True" AutoGenerateColumns="False"
                  OnCustomCallback="gvAccessoryDetail_CustomCallback" OnCustomJSProperties="gvAccessoryDetail_CustomJSProperties"
                  OnPageIndexChanged="gvAccessoryDetail_PageIndexChanged">
                  <Styles CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua">
                  </Styles>
                  <SettingsBehavior AllowDragDrop="False" AllowGroup="False" AllowSort="False" ConfirmDelete="True" />
                  <SettingsPager PageSize="20">
                    <AllButton>
                      <Image Height="19px" Width="27px" />
                    </AllButton>
                    <FirstPageButton>
                      <Image Height="19px" Width="23px" />
                    </FirstPageButton>
                    <LastPageButton>
                      <Image Height="19px" Width="23px" />
                    </LastPageButton>
                    <NextPageButton>
                      <Image Height="19px" Width="19px" />
                    </NextPageButton>
                    <PrevPageButton>
                      <Image Height="19px" Width="19px" />
                    </PrevPageButton>
                  </SettingsPager>
                  <Images ImageFolder="~/App_Themes/Aqua/{0}/">
                    <HeaderSortDown Height="5px" Url="~/App_Themes/Aqua/GridView/gvHeaderSortDown.png"
                      Width="7px" />
                    <ExpandedButton Height="15px" Url="~/App_Themes/Aqua/GridView/gvExpandedButton.png"
                      Width="15px" />
                    <CollapsedButton Height="15px" Url="~/App_Themes/Aqua/GridView/gvCollapsedButton.png"
                      Width="15px" />
                    <HeaderFilter Height="19px" Url="~/App_Themes/Aqua/GridView/gvHeaderFilter.png" Width="19px" />
                    <DetailCollapsedButton Height="15px" Url="~/App_Themes/Aqua/GridView/gvDetailCollapsedButton.png"
                      Width="15px" />
                    <DetailExpandedButton Height="15px" Url="~/App_Themes/Aqua/GridView/gvDetailExpandedButton.png"
                      Width="15px" />
                    <HeaderActiveFilter Height="19px" Url="~/App_Themes/Aqua/GridView/gvHeaderFilterActive.png"
                      Width="19px" />
                    <HeaderSortUp Height="5px" Url="~/App_Themes/Aqua/GridView/gvHeaderSortUp.png" Width="7px" />
                    <FilterRowButton Height="13px" Width="13px" />
                    <WindowResizer Height="13px" Url="~/App_Themes/Aqua/GridView/WindowResizer.png" Width="13px" />
                  </Images>
                  <SettingsLoadingPanel Mode="Disabled" />
                  <StylesEditors>
                    <ProgressBar Height="25px">
                    </ProgressBar>
                  </StylesEditors>
                  <ImagesEditors>
                    <CalendarFastNavPrevYear Height="19px" Url="~/App_Themes/Aqua/Editors/edtCalendarFNPrevYear.png"
                      Width="19px" />
                    <SpinEditLargeIncrement Height="9px" Url="~/App_Themes/Aqua/Editors/edtSpinEditLargeIncImage.png"
                      UrlDisabled="~/App_Themes/Aqua/Editors/edtSpinEditLargeIncDisabledImage.png" UrlHottracked="~/App_Themes/Aqua/Editors/edtSpinEditLargeIncHottrackedImage.png"
                      UrlPressed="~/App_Themes/Aqua/Editors/edtSpinEditLargeIncHottrackedImage.png" Width="7px" />
                    <SpinEditLargeDecrement Height="9px" Url="~/App_Themes/Aqua/Editors/edtSpinEditLargeDecImage.png"
                      UrlDisabled="~/App_Themes/Aqua/Editors/edtSpinEditLargeDecDisabledImage.png" UrlHottracked="~/App_Themes/Aqua/Editors/edtSpinEditLargeDecHottrackedImage.png"
                      UrlPressed="~/App_Themes/Aqua/Editors/edtSpinEditLargeDecHottrackedImage.png" Width="7px" />
                    <SpinEditIncrement Height="6px" Url="~/App_Themes/Aqua/Editors/edtSpinEditIncrementImage.png"
                      UrlDisabled="~/App_Themes/Aqua/Editors/edtSpinEditIncrementDisabledImage.png" UrlHottracked="~/App_Themes/Aqua/Editors/edtSpinEditIncrementHottrackedImage.png"
                      UrlPressed="~/App_Themes/Aqua/Editors/edtSpinEditIncrementHottrackedImage.png"
                      Width="7px" />
                    <SpinEditDecrement Height="7px" Url="~/App_Themes/Aqua/Editors/edtSpinEditDecrementImage.png"
                      UrlDisabled="~/App_Themes/Aqua/Editors/edtSpinEditDecrementDisabledImage.png" UrlHottracked="~/App_Themes/Aqua/Editors/edtSpinEditDecrementHottrackedImage.png"
                      UrlPressed="~/App_Themes/Aqua/Editors/edtSpinEditDecrementHottrackedImage.png"
                      Width="7px" />
                    <CalendarFastNavNextYear Height="19px" Url="~/App_Themes/Aqua/Editors/edtCalendarFNNextYear.png"
                      Width="19px" />
                    <DropDownEditDropDown Height="7px" Url="~/App_Themes/Aqua/Editors/edtDropDown.png"
                      UrlDisabled="~/App_Themes/Aqua/Editors/edtDropDownDisabled.png" UrlHottracked="~/App_Themes/Aqua/Editors/edtDropDownHottracked.png"
                      Width="9px" />
                  </ImagesEditors>
                  <Columns>
                    <dxwgv:GridViewDataColumn VisibleIndex="0" Caption="Tác vụ" Width="100px">
                      <DataItemTemplate>
                        <img alt="Cập nhật" title="Cập nhật" src="../Images/edit.png" style="cursor: pointer"
                          onclick="fillAccessoryDetail('<%#Eval("Id") %>', '<%#Eval("Name") %>', '<%#Eval("Quantity") %>', '<%#Eval("Amount") %>');" />
                        <img alt="Xóa" title="Xóa" src="../Images/delete.png" style="cursor: pointer" onclick="deleteAccessoryDetail('<%#Eval("Id") %>', '<%#Eval("Amount") %>');" />
                      </DataItemTemplate>
                      <HeaderStyle HorizontalAlign="Center" />
                      <CellStyle HorizontalAlign="Center" />
                    </dxwgv:GridViewDataColumn>
                    <dxwgv:GridViewDataTextColumn Caption="Tên nguyên vật liệu" VisibleIndex="1" FieldName="Name">
                      <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                    </dxwgv:GridViewDataTextColumn>
                    <dxwgv:GridViewDataTextColumn VisibleIndex="2" FieldName="Quantity" Caption="Số lượng">
                      <HeaderStyle HorizontalAlign="Center" />
                    </dxwgv:GridViewDataTextColumn>
                    <dxwgv:GridViewDataTextColumn Caption="Tổng tiền" VisibleIndex="3" FieldName="Amount">
                      <HeaderStyle HorizontalAlign="Center" />
                    </dxwgv:GridViewDataTextColumn>
                  </Columns>
                  <ClientSideEvents EndCallback="function(s, e){gvAccessoryDetail_EndCallback(s, e);}" />
                </dxwgv:ASPxGridView>
              </td>
            </tr>
            <tr>
              <td>
                <dxe:ASPxButton ID="btnNew" runat="server" Text="Thêm mới" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                  CssPostfix="Aqua" AutoPostBack="False">
                  <Image Url="~/Images/add.png" />
                  <ClientSideEvents Click="function(s, e) { addNewDetail();}" />
                </dxe:ASPxButton>
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td align="center">
          <dxe:ASPxButton ID="ASPxButton1" runat="server" Text="Lưu" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
            CssPostfix="Aqua" AutoPostBack="False">
            <Image Url="~/Images/save.png" />
            <ClientSideEvents Click="function(s, e) { onBtnSaveClick(s, e);}" />
          </dxe:ASPxButton>
        </td>
      </tr>
    </table>
    <dxpc:ASPxPopupControl ID="pcAccessoryDetail" runat="server" ClientInstanceName="pcAccessoryDetail"
      CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" HeaderText="Thông tin chi tiết hóa đơn"
      ImageFolder="~/App_Themes/Aqua/{0}/" Modal="True" PopupHorizontalAlign="WindowCenter"
      PopupVerticalAlign="WindowCenter">
      <ContentStyle VerticalAlign="Top">
      </ContentStyle>
      <SizeGripImage Height="12px" />
      <ContentCollection>
        <dxpc:PopupControlContentControl ID="PopupControlContentControl1" runat="server">
          <table width="270px" border="0">
            <tr>
              <td style="width: 100px">
                Tên:
              </td>
              <td style="width: 170px">
                <dxe:ASPxTextBox ID="txtDetailName" runat="server" ClientInstanceName="txtDetailName"
                  CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" Width="170px">
                  <ValidationSettings>
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                </dxe:ASPxTextBox>
              </td>
              <td class="notNullNote">
                (*)
              </td>
            </tr>
            <tr>
              <td>
                Số lượng:
              </td>
              <td>
                <dxe:ASPxTextBox ID="txtDetailQuantity" runat="server" ClientInstanceName="txtDetailQuantity"
                  CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" Width="170px">
                  <ValidationSettings>
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                </dxe:ASPxTextBox>
              </td>
              <td class="notNullNote">
                (*)
              </td>
            </tr>
            <tr>
              <td>
                Tổng tiền:
              </td>
              <td>
                <dxe:ASPxTextBox ID="txtDetailAmount" runat="server" ClientInstanceName="txtDetailAmount"
                  CssFilePath="~/App_Themes/Aqua/{0}/styles.css" CssPostfix="Aqua" Width="170px">
                  <MaskSettings IncludeLiterals="None" Mask="&lt;0..9999999999999999999g&gt;" />
                  <ValidationSettings Display="Dynamic">
                    <ErrorImage Height="14px" Url="~/App_Themes/Aqua/Editors/edtError.png" />
                    <ErrorFrameStyle ImageSpacing="4px">
                      <ErrorTextPaddings PaddingLeft="4px" />
                    </ErrorFrameStyle>
                  </ValidationSettings>
                </dxe:ASPxTextBox>
              </td>
              <td class="notNullNote">
                (*)
              </td>
            </tr>
            <tr>
              <td colspan="3" align="center" style="padding-top: 10px">
                <table>
                  <tr>
                    <td>
                      <dxe:ASPxButton ID="btnDetailSave" runat="server" Text="Lưu" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                        CssPostfix="Aqua" AutoPostBack="False">
                        <Image Url="~/Images/save.png" />
                        <ClientSideEvents Click="function(s, e) { onBtnDetailSaveClick();}" />
                      </dxe:ASPxButton>
                    </td>
                    <td>
                      <dxe:ASPxButton ID="btnDetailCancel" runat="server" Text="Hủy" CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
                        CssPostfix="Aqua" AutoPostBack="False">
                        <Image Url="~/Images/undo.gif" />
                        <ClientSideEvents Click="function(s, e) { pcAccessoryDetail.Hide();}" />
                      </dxe:ASPxButton>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </dxpc:PopupControlContentControl>
      </ContentCollection>
      <CloseButtonImage Height="16px" />
      <HeaderStyle Font-Bold="True" ForeColor="#59A1EF" />
    </dxpc:ASPxPopupControl>
  </div>
  <asp:HiddenField ID="hfAccessoryId" runat="server" />
  <asp:HiddenField ID="hfDetailId" runat="server" />

  <script type="text/javascript" language="javascript">
    var oldDetailAmount = 0;
    
    function onBack() {
      window.location.href = "AccessoryList.aspx";
    }

    function addNewDetail()
    {
      fillAccessoryDetail('', '', '', 0);
    }

    function fillAccessoryDetail(id, name, quantity, amount)
    {
      document.getElementById('<%=hfDetailId.ClientID %>').value = id;
      txtDetailName.SetText(name);
      txtDetailQuantity.SetText(quantity);
      txtDetailAmount.SetValue(amount);
      oldDetailAmount = amount;
      
      pcAccessoryDetail.Show();
    }
    
    function deleteAccessoryDetail(id, amount)
    {
      if (confirm('Bạn có thật sự muốn xóa không?'))
      {
        oldDetailAmount = amount;
        pcWaiting.Show();
        gvAccessoryDetail.PerformCallback('DeleteDetail|' + id);
      }
    }
    
    function checkAccessoryPara()
    {
      if (deImportDate.GetValue() == null)
      {
        alert('Vui lòng nhập ngày nhập nguyên vật liệu.');
        return false;
      }
      
//      if (txtTotalAmount.GetValue() <= 0)
//      {
//        alert('Tổng tiền phải lớn hơn 0.');
//        return false;
//      }
      
      return true;
    }
    
    function checkDetailPara()
    {
      if (txtDetailName.GetText() == '')
      {
        alert('Vui lòng nhập tên nguyên vật liệu.');
        return false;
      }
      
      if (txtDetailQuantity.GetText() == '')
      {
        alert('Vui lòng nhập số lượng nguyên vật liệu.');
        return false;
      }
      
      if (txtDetailAmount.GetValue() <= 0)
      {
        alert('Tổng tiền nguyên vật liệu phải lớn hơn 0.');
        return false;
      }
      
      return true;
    }
    
    function onBtnDetailSaveClick()
    {
      if (checkDetailPara())
      {
        pcWaiting.Show();
        gvAccessoryDetail.PerformCallback('SaveDetail');
      }
    }
    
    function onBtnSaveClick(s, e) {
      if (checkAccessoryPara())
      {
        pcWaiting.Show();
        gvAccessoryDetail.PerformCallback('Save');
      }
    }
    
    function gvAccessoryDetail_EndCallback(s, e)
    {
      pcWaiting.Hide();
      var error = gvAccessoryDetail.cpInnerError.split('|');
      switch(error[0])
      {
        case 'DeleteDetail':
          var totalAmount = txtTotalAmount.GetValue();
          txtTotalAmount.SetValue(String(parseInt(totalAmount) - parseInt(oldDetailAmount)));
          break;
        case 'SaveDetail':
          var totalAmount = txtTotalAmount.GetValue();
          var newDetailAmount = txtDetailAmount.GetValue();
          txtTotalAmount.SetValue(String(parseInt(totalAmount) + parseInt(newDetailAmount) - parseInt(oldDetailAmount)));
          pcAccessoryDetail.Hide();
          break;
        case 'Save':
          document.getElementById('<%=hfAccessoryId.ClientID %>').value = error[1];
          txtRefNo.SetTex(error[2]);
          alert('Dữ liệu đã được cập nhật thành công.');
          break;
        default:
          alert(error[0]);
          break;
      }
    }
  </script>

</asp:Content>
